<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;

/**
 * 客户配置管理
 *
 * @icon fa fa-circle-o
 */
class Config extends Backend
{
    
    /**
     * Config模型对象
     * @var \app\admin\model\user\Config
     */
    protected $model = null;
    protected $modelValidate = true;
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\user\Config;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->withJoin(['user'],'left')
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->withJoin(['user'],'left')
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            // echo $this->model->getlastsql();die;
            // foreach ($list as $row) {
            //     $row->visible(['id','user_id','dpi','createtime','updatetime']);
                
            // }
            $list = $list->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 详情
     */
    public function detail($ids)
    {
        $this->relationSearch = true;
        $row = $this->model->withJoin(['user'],'left')->get(['config.id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        $data = $row->toArray();
        $data['user_id'] = $data['user']['nickname'];
        $data['创建时间'] = date("Y-m-d H:i:s",$data['createtime']);
        $data['修改时间'] = date("Y-m-d H:i:s",$data['updatetime']);
        unset($data['createtime']);
        unset($data['updatetime']);
        unset($data['user']);
        $this->view->assign("row", $data);
        return $this->view->fetch();
    }
    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        // 5.0框架升级至5.1框架修改部分
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = new $name;
                        if(!$validate->scene('edit')->check($params)){
                            $this->error($validate->getError());
                        }
                        // $name = basename(str_replace('\\', '/', get_class($this->model)));
                        // $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        // $row->validate($validate);
                    }
                    $result = $row->allowField(true)->force()->save($params);//对于没有变化的数据是不会进行重新更新的。如果你需要强制更新数据使用force()方法
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $dpi = json_decode($row['dpi'],true);
        $row['dpi'] = json_encode($dpi,JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
        if(!empty($row['probe']) && is_json($row['probe'])){
            $probe = json_decode($row['probe'],true);
            $row['probe'] = json_encode($probe,JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
        }
        if($row['dpi'] == 'null'){
            $row['dpi'] = '';
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
}
